<?php
/**
 * Created by PhpStorm.
 * User: yzapple
 * Date: 2018/1/11
 * Time: 下午4:09
 */

namespace app\common\model;


use app\common\model\Base;
use think\Db;
class Patient extends Base
{

    protected $table = "patient";
    protected $base_arr = [
        'table' => 'patient',
        'field' => 'name,sex,document_type,document_num,birth,phone,avatar,doctor_id',
        'primary_key' => 'id'
    ];

    protected $attach_arr =  [
        [
            'table' => 'doctor',
            'field' => 'name',
            'primary_key' => 'id',
            'base_table_field' => 'doctor_id'
        ],

    ];

    /**
     * 获取字段对应的数组
     * */
    public function getDataByMysql(){
        $data = [
            't_name' => $this->table,//必填
            'model' => '患者',//必填
            'name' => '姓名',
            'sex' => '{"1":"男","2":"女"}',
            'document_type' => '{"1":"身份证","2":"护照","3":"港澳通行证"}',
            'document_num' => '证件号',
            'birth' => '出生日期',
            'phone' => '联系方式',
            'avatar' => '头像',
            'doctor_id' => '录入医生',
            'is_deleted' => '{"2":"功能禁用","1":"功能启用","3":"功能删除"}'
        ];
        return $data;
    }


    /**
     * 获取日志的访问方法
     **/
    public static function getPatientAdminLog($list_action='',$form_data=[],$userId,$admin_type){
        $model = new Patient();
        $actionName = strtolower(ACTION_NAME);
        $controller = strtolower(CONTROLLER_NAME);
        $old_data = [];
        $type = '';

        switch($actionName){
            //列表
            case "index":
                Base::save_admin_log($userId, $model, 'check','',[],[],$admin_type);
                break;
            case "check_detail":
//                $form_data['doctor_id'] = $userId;
                if($model->request->isGet()){
                $record_id = $model->request->param('cid');

            }
                Base::save_admin_log($userId,$model,'check',$record_id);
                break;
            case "add_patient":
                $type = 'add';
                if ($model->request->isPost()) {
                    //记录当前登陆者的操作
                    //身份证号去重
                    $form_data['doctor_id'] = $userId;
                    $list_action = 'patient_info';
                }
                break;
            case "edit_patient":
                $type = 'edit';
                if ($model->request->isPost()) {
                    $old_data = Base::getOneOldDataByTables($form_data['id'],$model->base_arr,$model->attach_arr);
                    //后台编辑
                   // $form_data['doctor_id'] = $userId;
                }
                break;
            case "del_patient":
                $type = 'del';
                if ($model->request->isPost()) {
                    $id = $form_data["id"];
                    Db::execute("update patient set is_deleted=3  where id=?", [ $id]);

                }
                break;
        }


        if($model->request->isPost()){

            //$model 实例化之后的模型
            //$type 数据的操作类型
            //$form_data  form表单提交之后的表单
            //$url 跳转路径
            //$old_data 原有的数据
            //$attach_arr 附属表单数组
            //$primary_key 基本表单的主键值id

            $result =  Base::operaTable($model,$userId,$type,$form_data,$controller.'/'.$list_action,$old_data,$model->attach_arr,'id',$admin_type);
            return $result;
        }


        return true;


    }

}
